package com.szholly.data.general;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
 * 定义更新对象
 */
public class UpdateMap implements IUpdateMap {

	private HashMap<String, Object> setMap = new HashMap<String, Object>();
	private HashMap<String, Object> whereMap = new HashMap<String, Object>();

	@Override
	public void setSetMap(HashMap<String, Object> setMap) {
		this.setMap = setMap;
	}

	@Override
	public HashMap<String, Object> getSetMap() {
		return setMap;
	}

	@Override
	public HashMap<String, Object> getWhereMap() {
		return whereMap;
	}

	@Override
	public void setWhereMap(HashMap<String, Object> whereMap) {
		this.whereMap = whereMap;
	}

	@Override
	public IUpdateFilter toUpdateFilter() {
		IUpdateFilter updateFilter = new UpdateFilter();
		List<GlobalParameter> glo = new ArrayList<GlobalParameter>();
		String setString = "";
		Set<String> keys = setMap.keySet();
		for(String key : keys){
			setString+= key + "=@"+key+",";
			glo.add(new GlobalParameter("@"+key, setMap.get(key)));
		}
		if(setString!=""){
			setString = setString.substring(0, setString.length()-1);
		}
		updateFilter.setSetFields(setString);
		
		String whereString = "";
		if (whereMap != null) {
			keys = whereMap.keySet();
			for (String key : keys) {
				String[] fields = key.split(":");
				String field = null;
				String option = null;
				if (fields.length == 2) {
					field = fields[0];
					option = fields[1];
				} else {
					field = key;
					option = "=";
				}

				Object value = whereMap.get(key);
				if (option.equals("=")) {
					if (value == null || value.equals("")) {
						continue;
					}
					if (value.equals("\"null\"")) {
						whereString += field + " is null and ";
					} else {
						whereString += field + " = @" + field + " and ";
						glo.add(new GlobalParameter("@" + field, value));
					}
				} else if (option.equals(">")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " > @" + field + " and ";
					glo.add(new GlobalParameter("@" + field, value));
				} else if (option.equals("!=")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " != @" + field + " and ";
					glo.add(new GlobalParameter("@" + field, value));
				} else if (option.equals(">=")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " >= @" + field + " and ";
					glo.add(new GlobalParameter("@" + field, value));
				} else if (option.equals("<")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " < @" + field + " and ";
					glo.add(new GlobalParameter("@" + field, value));
				} else if (option.equals("<=")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " <= @" + field + " and ";
					glo.add(new GlobalParameter("@" + field, value));
				} else if (option.equals("in")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " in (" + value.toString()
							+ ") and ";
				} else if (option.equals("nin")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " not in (" + value.toString()
							+ ") and ";
				} else if (option.equals("isnull")) {
					whereString += field + " is null and ";
				} else if (option.equals("isnnull")) {
					whereString += field + " is not null and ";
				} else if (option.equals("like")) {
					if (value == null || value.equals("")) {
						continue;
					}
					whereString += field + " like '%" + value.toString()
							+ "%' and ";
				} else {
					throw new RuntimeException("不支持的操作符【" + option + "】！");
				}
			}
		}
		if (whereString != "") {
			whereString = whereString.substring(0, whereString.length() - 4);
		}
		updateFilter.setWhereString(whereString);
		updateFilter.setWhereParameter(glo.toArray(new GlobalParameter[glo.size()]));
		return updateFilter;
	}

	@Override
	public Object getWhereObject() {
//		DBObject queryCondition = new BasicDBObject();
//		if(whereMap==null){
//			return queryCondition;
//		}
//		Set<String> keys = whereMap.keySet();
//		for (String key : keys) {
//			String[] fields = key.split(":");
//			String field = null;
//			String option = null;
//			if (fields.length == 2) {
//				field = fields[0];
//				option = fields[1];
//			} else {
//				field = key;
//				option = "=";
//			}
//
//			Object value = whereMap.get(key);
//			if (option.equals("=")) {
//				if(value==null || value.equals("")){
//					continue;
//				}
//				if (value.equals("\"null\"")) {
//					queryCondition.put(field, null);
//				} else {
//					queryCondition.put(field, value);
//				}
//			} else if (option.equals(">")) {
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBObject v = new BasicDBObject("$gt", value);
//				queryCondition.put(field, v);
//			} else if (option.equals("!=")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBObject v = new BasicDBObject("$ne", value);
//				queryCondition.put(field, v);
//			} else if (option.equals(">=")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBObject v = new BasicDBObject("$gte", value);
//				queryCondition.put(field, v);
//			} else if (option.equals("<")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBObject v = new BasicDBObject("$lt", value);
//				queryCondition.put(field, v);
//			} else if (option.equals("<=")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBObject v = new BasicDBObject("$lte", value);
//				queryCondition.put(field, v);
//			} else if (option.equals("in")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBList values = new BasicDBList();
//				String[] valueStrings = value.toString().split(",");
//				for(String item : valueStrings){
//					values.add(item);
//				}
//				queryCondition.put(field, new BasicDBObject("$in", values));
//			} else if (option.equals("nin")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				BasicDBList values = new BasicDBList();
//				String[] valueStrings = value.toString().split(",");
//				for(String item : valueStrings){
//					values.add(item);
//				}
//				queryCondition.put(field, new BasicDBObject("$nin", values));
//			} else if (option.equals("isnull")){
//				queryCondition.put(field, null);
//			} else if (option.equals("isnnull")){
//				BasicDBObject v = new BasicDBObject("$ne", null);
//				queryCondition.put(field, v);
//			} else if (option.equals("like")){
//				if(value==null || value.equals("")){
//					continue;
//				}
//				Pattern pattern = Pattern.compile("^.*" + value.toString() + ".*$",
//						Pattern.CASE_INSENSITIVE);
//				queryCondition.put(field, pattern);
//			} else {
//				throw new RuntimeException("不支持的操作符【"+option+"】！");
//			}
//		}

		return null;
	}

	@Override
	public Object getSetObject() {
//		DBObject setCondition = new BasicDBObject();
//		setCondition.putAll(setMap);
//		DBObject allCondition = new BasicDBObject();
//		allCondition.put("$set", setCondition);
		return null;
	}
}